package com.woniu.tank.tank;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.Random;
import java.util.Vector;

import javax.swing.JPanel;

import com.woniu.tank.bullet.BadBullet;
import com.woniu.tank.ui.GamePanel;

public class BadTank extends Tank implements Runnable{
	//添加子弹时间间隔
	private int tm;
	//星星线程
	public boolean isDrawStar = true;
	//	用于停止坦克的线程
	public boolean isDrawTank = true;	
	//	坦克的动态效果
	private int starId;
	//定义坦克随机生成
	Random rd = new Random();
	int flag = rd.nextInt(8);
	//随机移动方向
	int liberty = rd.nextInt(4);
	//生成坦克位置
	int po = rd.nextInt(3);

	BadTank badt = null;


	public BadTank() {
	
	}
	public BadTank(int x,int y){
		super(x,y);
		
	}

	//默认坦克方向
	public BadTank(int x, int y,GamePanel tc) {
		super(x,y,tc);
		this.dir=2;
		this.step=0;
		new Thread(this).start();
	}
	//	绘制坦克出生前闪烁的星星
	public void draw(Graphics g, JPanel p ,Image star , Image enemy ,Image image){
		if(isDrawStar){
			drawStar(g, p, star);
		}
		else{
			drawBad(g, enemy, p, image);					
		}
	}
	//	绘制星星的闪烁效果
	public void drawStar(Graphics g , JPanel p ,Image star){
		if(starId >=3){
			starId = 0;
		}else{
			starId ++;
		}
		g.drawImage(star, x*32, y*32, (x+1)*32, (y+1)*32, starId*32, 0, (starId+1)*32, 32, p);	
	}
	//产生随机的敌方坦克
	public void drawBad(Graphics g, Image enemy, JPanel p , Image image){ 		
		switch(flag){
		case 0:
			g.drawImage(enemy, x*32, y*32, (x+1)*32, (y+1)*32, (step+6)*28, (dir+4)*28, (step+7)*28, (dir+5)*28, p);
			break;
		case 1:
			g.drawImage(enemy, x*32, y*32, (x+1)*32, (y+1)*32, step*28, (dir+4)*28, (step+1)*28, (dir+5)*28, p);
			break;
		case 2:
			g.drawImage(enemy, x*32, y*32, (x+1)*32, (y+1)*32, (step+2)*28, (dir+4)*28, (step+3)*28, (dir+5)*28, p);
			break;
		case 3:
			g.drawImage(enemy, x*32, y*32, (x+1)*32, (y+1)*32, (step+4)*28, (dir+4)*28, (step+5)*28, (dir+5)*28, p);
			break;
		case 4:
			g.drawImage(enemy, x*32, y*32, (x+1)*32, (y+1)*32, step*28, dir*28,(step+1)*28, (dir+1)*28, p);
			break;
		case 5:
			g.drawImage(enemy, x*32, y*32, (x+1)*32, (y+1)*32, (step+2)*28, dir*28,(step+3)*28, (dir+1)*28, p);
			break;
		case 6:
			g.drawImage(enemy, x*32, y*32, (x+1)*32, (y+1)*32, (step+4)*28, dir*28,(step+5)*28, (dir+1)*28, p);
			break;
		case 7:
			g.drawImage(enemy, x*32, y*32, (x+1)*32, (y+1)*32, (step+6)*28, dir*28,(step+7)*28, (dir+1)*28, p);
			break;
		}										
		step();
		underGrass(g, p, image);
	}

	//	构造一个产生随机数的函数
	private void TankeMove(){
		liberty = rd.nextInt(4);
	}
	//坦克开火
	private BadBullet fir(){
		BadBullet bb = new BadBullet(x, y, dir,tc);
		if(tm>3){
			tm=0;
			GamePanel.bbullet.add(bb);
		}
		tm++;	
		return bb;		
	}
	//坦克随机在地图上面移动
	public void move(int[][] map){
		switch(liberty){
		case 0:
			if(dir !=0){
				dir = 0;
			}else if(map[y-1][x]== 0 || map [y-1][x] ==3 ){
				y--;
				fir();				
			}else{
				TankeMove();
			}
			break;
		case 1:
			if(dir !=1){
				dir=1;
			}else if(map[y][x+1]==0 || map [y][x+1] ==3 ){				
				x++;
				fir();				
			}else{
				TankeMove();
			}
			break;
		case 3:
			if(dir!=3){
				dir=3;

			}else if(map[y][x-1]==0 || map [y][x-1] ==3){
				x--;
				fir();				
			}else{
				TankeMove();
			}
			break;
		case 2:
			if(dir!=2){
				dir=2;				
			}else if(map[y+1][x]== 0 || map [y+1][x] ==3){
				y++;
				fir();
			}else{
				TankeMove();
			}
			break;
		}
	}
	//随机三个数产生固定三个位置.循环产生坦克
	private void createTank(){		
		if(GamePanel.tanktk.size()<6){			
			switch(po){
			case 0:
				badt= new BadTank(1, 1,tc);
				GamePanel.tanktk.add(badt);
				break;
			case 1:
				badt= new BadTank(13, 1,tc);
				GamePanel.tanktk.add(badt);
				break;
			case 2:
				badt= new BadTank(24, 1,tc);
				GamePanel.tanktk.add(badt);
				break;
			}
			
		}
	}
	//构造一个矩形
	public Rectangle getRect(){
		return new Rectangle(x,y,1,1);
	}
	//坦克相交后,换随机换方向。
	public void collisionwithTank(){
		for(int i=0;i<GamePanel.tanktk.size();i++){
			BadTank tanks = GamePanel.tanktk.get(i);
			if(this!=tanks){
				if(this.getRect().intersects(tanks.getRect())){
					TankeMove();
				}
			}
		}
	}
	//	坦克移动时间间隔
	@Override
	public void run() {		
		int startTime = 0 ;
		while(isDrawTank){
			if(isDrawStar){
				startTime++;
				if(startTime>4){
					isDrawStar = false;				
				}
				try {
					Thread.sleep(400);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
			}else{			
				this.collisionwithTank();
				this.move(GamePanel.map);							
				try {	
					createTank();
					Thread.sleep(700);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}
